package com.takeaway.ui.fragment

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.takeaway.ui.adapter.HomeRcAdapter
import com.takeaway.R
import com.takeaway.dagger2.component.DaggerHomeFragmentComponent
import com.takeaway.dagger2.component.HomeFragmentComponent
import com.takeaway.dagger2.module.HomeFragmentModule
import com.takeaway.presenter.HomeFragmentPresenter
import dagger.internal.DaggerCollections
import kotlinx.android.synthetic.main.fragment_home.*
import javax.inject.Inject


class HomeFragment : Fragment() {

    private lateinit var homeRcAdapter: HomeRcAdapter
    private lateinit var rvHome: RecyclerView

    @Inject
    lateinit var homePresneter: HomeFragmentPresenter

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = View.inflate(activity as Context, R.layout.fragment_home, null)

        Log.e("HomeFragment", "初始化")
        rvHome = view.findViewById<RecyclerView>(R.id.rv_home)
        rvHome.layoutManager = LinearLayoutManager(activity)
        homeRcAdapter = HomeRcAdapter(activity as Context)
        rvHome.adapter = homeRcAdapter
        distance = 120.dpToPx()
        // 解耦view层和p层 通过依赖注入
//        homePresneter = HomeFragmentPresenter(this)
        DaggerHomeFragmentComponent.builder().homeFragmentModule(HomeFragmentModule(this)).build().inject(this)
        return view
    }


    fun Int.dpToPx() : Int {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, toFloat(), resources.displayMetrics).toInt()
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initData()
    }

    var datas: ArrayList<String> = ArrayList()
    private var sum: Int = 0 /// 总偏移距离
    private var alpha = 55
    // 最大偏移距离
    var distance: Int = 0
    private fun initData() {
        for (i in 0 until 50) {
            datas.add("测试数据 - " + i)
        }

        homePresneter.getHomeInfo()

        homeRcAdapter.setData(datas)

        // 添加滚动监听
        rvHome.addOnScrollListener(object: RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                sum += dy
                if (sum > distance) {
                    alpha = 255
                } else {
                    alpha = sum / distance * 200
                    alpha += 55
                }
                // 553190E8
                ll_title_container.setBackgroundColor(Color.argb(alpha, 0x31, 0x90, 0xE8))
            }
        })
    }

    fun successData() {
        // 获取数据成功
    }

    fun failData() {
        // 获取数据失败
    }
}